package com.augurit.gzcgwsp.readAndParseXML.service.impl;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.gzcgwsp.readAndParseXML.service.ICsdlwjService;
import com.augurit.gzcgwsp.readAndParseXML.service.IParseXMLService;
import com.augurit.gzcgwsp.smartformCsdlwj.service.IYwCsdlwjService;
import com.augurit.gzcgwsp.smartformCsdlwj.web.form.CsdlwjForm;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComQyxxForm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *  城市道路挖掘 交换
 * @author Administrator
 *
 */
@Service
@Transactional
public class CsdlwjServiceImpl implements ICsdlwjService {
	Logger logger = (Logger) LoggerFactory.getLogger(CsdlwjServiceImpl.class);
	@Autowired
	private IParseXMLService parseXMLService;

	@Autowired
	private IYwCsdlwjService ywCsdlwjService;
	
	Locale locale = Locale.getDefault(); 
	ResourceBundle localResource = ResourceBundle.getBundle("interfaceExchange", locale); 

	// xml存放路径
	private final String csdlwjfilepath = localResource.getString("csdlwjfilepath");

	// 附件存放路径
	private final String csdlwjfujianpath = localResource.getString("csdlwjfujianpath");

	// 解析成功反馈xml存放路径
	private final String csdlwjfeedbackPath = localResource.getString("csdlwjfeedbackPath");

	// 解析异常xml存放路径
	private final String csdlwjexceptionPath = localResource.getString("csdlwjexceptionPath");

	// 解析后，xml备份
	private final String csdlwjbackPath = localResource.getString("csdlwjbackPath");

	private Map<String, String> csdlwjKeyMap;// xml内容的key

	private Map<String, String> csdlwjKeyMapHead;// xml头部的key

	private Map<String, Long> csdlwjfujianMap;// 附件的别名和id对应的map

	private String errorCode = "";// 异常编号

	private String errorStatus = "";// 异常描述

	private String errorStack = "";// 异常堆栈

	private void inithwShMap() {
		// 初始化数据部分对应的map
		csdlwjKeyMap = new HashMap<String, String>();
		
		csdlwjKeyMap.put("TBRQ", "TBRQ");
		csdlwjKeyMap.put("JSDW", "JSDW");
		csdlwjKeyMap.put("JSDW_ADDRESS", "JSDW_ADDRESS");
		csdlwjKeyMap.put("JSDW_TEL", "JSDW_TEL");
		csdlwjKeyMap.put("FDDBR", "FDDBR");
		csdlwjKeyMap.put("LXR", "LXR");
		csdlwjKeyMap.put("SGDW", "SGDW");
		csdlwjKeyMap.put("SGDW_ADDRESS", "SGDW_ADDRESS");
		csdlwjKeyMap.put("SGDW_TEL", "SGDW_TEL");
		csdlwjKeyMap.put("AQFZR", "AQFZR");
		csdlwjKeyMap.put("SGDW_LXR", "SGDW_LXR");
		csdlwjKeyMap.put("SCXPZWH", "SCXPZWH");
		csdlwjKeyMap.put("WJDLMQZLD", "WJDLMQZLD");
		csdlwjKeyMap.put("RXD_LENGTH", "RXD_LENGTH");
		csdlwjKeyMap.put("RXD_WIDTH", "RXD_WIDTH");
		csdlwjKeyMap.put("GGCS_LENGTH", "GGCS_LENGTH");
		csdlwjKeyMap.put("GGCS_WIDTH", "GGCS_WIDTH");
		csdlwjKeyMap.put("SFWMNX", "SFWMNX");
		csdlwjKeyMap.put("SFWJCXD", "SFWJCXD");
		csdlwjKeyMap.put("SFZDDL", "SFZDDL");		
		csdlwjKeyMap.put("WJJHAP", "WJJHAP");		
		csdlwjKeyMap.put("QYGLBMYJ", "QYGLBMYJ");
		csdlwjKeyMap.put("GAJTGLBMYJ", "GAJTGLBMYJ");		
		csdlwjKeyMap.put("TXYJRQ", "TXYJRQ");
		csdlwjKeyMap.put("WJLB0", "WJLB0");
		csdlwjKeyMap.put("WJLB1", "WJLB1");
		csdlwjKeyMap.put("WJLB2", "WJLB2");
		csdlwjKeyMap.put("WJLB3", "WJLB3");
		csdlwjKeyMap.put("WJLB4", "WJLB4");
		csdlwjKeyMap.put("WJLB5", "WJLB5");
		csdlwjKeyMap.put("WJLB6", "WJLB6");
		csdlwjKeyMap.put("WJLB7", "WJLB7");
		csdlwjKeyMap.put("WJLB8", "WJLB8");
		csdlwjKeyMap.put("QYGLBMYJ", "QYGLBMYJ");
		
		csdlwjKeyMap.put("CBYW", "CBYW");
		csdlwjKeyMap.put("sendTime", "sendTime");
		
		csdlwjKeyMap.put("YSL_NO", "YSL_NO");
		csdlwjKeyMap.put("approvalNo", "approvalNo");
		csdlwjKeyMap.put("businessCode", "businessCode");

		// 初始化头部对应的map
		csdlwjKeyMapHead = new HashMap<String, String>();
		csdlwjKeyMapHead.put("sn", "sn");
		csdlwjKeyMapHead.put("sender", "sender");
		csdlwjKeyMapHead.put("businessCode", "businessCode");

		// 初始化附件的别名和id对应的map
		csdlwjfujianMap = new HashMap<String, Long>();
		csdlwjfujianMap.put("FJ_01", 301l);
		csdlwjfujianMap.put("FJ_02", 302l);
		csdlwjfujianMap.put("FJ_03", 303l);
		csdlwjfujianMap.put("FJ_04", 304l);
		csdlwjfujianMap.put("FJ_05", 305l);
		csdlwjfujianMap.put("FJ_06", 306l);
		csdlwjfujianMap.put("FJ_07", 307l);
		csdlwjfujianMap.put("FJ_08", 308l);
	}

	public void readXml(String projectPath) {
		if (csdlwjKeyMap == null)
			this.inithwShMap();
		File file = new File(csdlwjfilepath);
		File[] files = file.listFiles();
		if (files != null && files.length > 0) {

			for (int i = 0; i < files.length; i++) {
				if (files[i].isFile()) {
					String filePath = files[i].getPath();
					String fileName = files[i].getName();
					try {
						// 解析xml内容
						parseXMLService.initXmlFile(csdlwjKeyMap, filePath,csdlwjfeedbackPath, csdlwjexceptionPath);
						// 解析xml头部内容
						parseXMLService.initXmlFileHead(csdlwjKeyMapHead,files[i], csdlwjfeedbackPath, csdlwjexceptionPath);

						// 把数据部分保存到数据库
						CsdlwjForm form = new CsdlwjForm();
						for (String mapKey : csdlwjKeyMap.keySet()) {

							// TODO 把值放到form中
							form.setJsdw(parseXMLService.getMapValue(csdlwjKeyMap.get("JSDW")));	
							System.out.println("JSDW!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"+parseXMLService.getMapValue(csdlwjKeyMap.get("JSDW")));
							YwComQyxxForm ywComQyxxForm = parseXMLService.getSqdwBySqdw(parseXMLService.getMapValue(csdlwjKeyMap.get("JSDW")),"csdlwj");
							if(ywComQyxxForm != null){
								form.setQybh(ywComQyxxForm.getId().toString());
							}
							
							String TBRQJTemp = parseXMLService.getMapValue(csdlwjKeyMap.get("TBRQ"));
							Date TBRQ = null;
							if (org.apache.commons.lang.StringUtils.isNotEmpty(TBRQJTemp)) {
								TBRQ = DateUtils.stringToDate(TBRQJTemp, DateUtils.YYYY_MM_DD);
								form.setTbrq(TBRQ);
							}
														
							
							form.setJsdwAddress(parseXMLService.getMapValue(csdlwjKeyMap.get("JSDW_ADDRESS")));
							form.setJsdwTel(parseXMLService.getMapValue(csdlwjKeyMap.get("JSDW_TEL")));
							form.setFddbr(parseXMLService.getMapValue(csdlwjKeyMap.get("FDDBR")));
							form.setLxr(parseXMLService.getMapValue(csdlwjKeyMap.get("LXR")));
							form.setSgdw(parseXMLService.getMapValue(csdlwjKeyMap.get("SGDW")));
							form.setSgdwAddress(parseXMLService.getMapValue(csdlwjKeyMap.get("SGDW_ADDRESS")));
							form.setSgdwTel(parseXMLService.getMapValue(csdlwjKeyMap.get("SGDW_TEL")));
							form.setAqfzr(parseXMLService.getMapValue(csdlwjKeyMap.get("AQFZR")));
							form.setSgdwLxr(parseXMLService.getMapValue(csdlwjKeyMap.get("SGDW_LXR")));
							form.setScxpzwh(parseXMLService.getMapValue(csdlwjKeyMap.get("SCXPZWH")));
							form.setWjdlmqzld(parseXMLService.getMapValue(csdlwjKeyMap.get("WJDLMQZLD")));
							form.setRxdLength(parseXMLService.getMapValue(csdlwjKeyMap.get("RXD_LENGTH")));
							form.setRxdWidth(parseXMLService.getMapValue(csdlwjKeyMap.get("RXD_WIDTH")));
							form.setGgcsLength(parseXMLService.getMapValue(csdlwjKeyMap.get("GGCS_LENGTH")));
							form.setGgcsWidth(parseXMLService.getMapValue(csdlwjKeyMap.get("GGCS_WIDTH")));
							
							String sfwmnx = parseXMLService.getMapValue(csdlwjKeyMap.get("SFWMNX"));
							if(StringUtils.isNotBlank(sfwmnx)){
								if("是".equals(sfwmnx)){
									sfwmnx = "1";
								}else{
									sfwmnx = "2";
								}
								form.setSfwmnx(sfwmnx);
							}
							
							String sfwjcxd = parseXMLService.getMapValue(csdlwjKeyMap
									.get("SFWJCXD"));
							if(StringUtils.isNotBlank(sfwjcxd)){
								if("是".equals(sfwjcxd)){
									sfwjcxd = "1";
								}else{
									sfwjcxd = "2";
								}
								form.setSfwjcxd(sfwjcxd);
							}
							
							String sfzddl = parseXMLService.getMapValue(csdlwjKeyMap.get("SFZDDL"));
							if(StringUtils.isNotBlank(sfzddl)){
								if("是".equals(sfzddl)){
									sfzddl = "1";
								}else{
									sfzddl = "2";
								}
								form.setSfzddl(sfzddl);
							}
							form.setWjjhap(parseXMLService.getMapValue(csdlwjKeyMap.get("WJJHAP")));
							form.setQyglbmyj(parseXMLService.getMapValue(csdlwjKeyMap.get("QYGLBMYJ")));
							form.setGajtglbmyj(parseXMLService.getMapValue(csdlwjKeyMap.get("GAJTGLBMYJ")));
							
							form.setWjlb0(parseXMLService.getMapValue(csdlwjKeyMap.get("WJLB0")));
							form.setWjlb1(parseXMLService.getMapValue(csdlwjKeyMap.get("WJLB1")));
							form.setWjlb2(parseXMLService.getMapValue(csdlwjKeyMap.get("WJLB2")));
							form.setWjlb3(parseXMLService.getMapValue(csdlwjKeyMap.get("WJLB3")));
							form.setWjlb4(parseXMLService.getMapValue(csdlwjKeyMap.get("WJLB4")));
							form.setWjlb5(parseXMLService.getMapValue(csdlwjKeyMap.get("WJLB5")));
							form.setWjlb6(parseXMLService.getMapValue(csdlwjKeyMap.get("WJLB6")));
							form.setWjlb7(parseXMLService.getMapValue(csdlwjKeyMap.get("WJLB7")));
							form.setWjlb8(parseXMLService.getMapValue(csdlwjKeyMap.get("WJLB8")));
							
							form.setQyglbmyj(parseXMLService.getMapValue(csdlwjKeyMap.get("QYGLBMYJ")));
							
							String TXYJRQTemp = parseXMLService.getMapValue(csdlwjKeyMap.get("TXYJRQ"));
							Date TXYJRQ = null;
							if (org.apache.commons.lang.StringUtils.isNotEmpty(TXYJRQTemp)) {
								TXYJRQ = DateUtils.stringToDate(TXYJRQTemp, DateUtils.YYYY_MM_DD);
								form.setTxyjrq(TXYJRQ);
							}
							
							form.setCbyw("城市道路人行道及相关公共场地挖掘审批");
							form.setBranch("csdlwj");
							form.setYsl(1l);
							form.setSendNotice("Y");
							form.setYslNo(parseXMLService.getMapValue(csdlwjKeyMap.get("YSL_NO")));
							form.setApprovalNo(parseXMLService.getMapValue(csdlwjKeyMap.get("approvalNo")));
							form.setBusinessCode(parseXMLService.getMapValue(csdlwjKeyMap.get("businessCode")));
							
							String wstjsjStr = parseXMLService.getMapValue(csdlwjKeyMap.get("sendTime"));
							Date wstjsj = null;
							if (StringUtils.isNotEmpty(wstjsjStr)) {
								wstjsj = DateUtils.stringToDate(wstjsjStr, DateUtils.YYYY_MM_DD_HH_MM_SS);
								form.setWstjsj(wstjsj);
							}
						}
						// 调用DAO，保存到数据库
						try {
							form.setSqdw(parseXMLService.getMapValue(csdlwjKeyMap.get("SQDW")));
							ywCsdlwjService.save(form);
						} catch (Exception e) {
							// TODO 生成异常xml
							e.printStackTrace();
							errorCode = "1001";
							errorStatus = "插入数据库失败";
							parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
									csdlwjKeyMapHead, csdlwjfeedbackPath, csdlwjexceptionPath, i);
						}

						//处理附件
						parseXMLService.handleFj(projectPath, fileName, csdlwjfujianpath, csdlwjfeedbackPath, csdlwjexceptionPath, csdlwjKeyMapHead,
								files, csdlwjfujianMap, i, Integer.parseInt(form.getQybh()), form.getId(), "csdlwj");
					} catch (Exception e) {
						// TODO 生成异常xml
						e.printStackTrace();
						errorCode = "1000";
						errorStatus = "解析xml失败";
						parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
								csdlwjKeyMapHead, csdlwjfeedbackPath, csdlwjexceptionPath, i);
					}
					// 剪切文件,把文件移走
					parseXMLService.removeXML(filePath, csdlwjbackPath + fileName,
							fileName);
				}
			}
		}
	}

}
